package command;

import java.sql.SQLException;
import java.util.Date;

import human.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import jdbc.UserDao;
import logic.Localizator;

public class HomeCommand implements Command {

	private static final Logger log = Logger.getLogger(HomeCommand.class.getName());

	@Override
	public String execute(HttpServletRequest request, HttpServletResponse response) {
		UserDao userDao = new UserDao();

		String page = null;
		try {
			userDao.close();
			userDao.start();
			HttpSession sess = request.getSession(false);
			User user = userDao.selectUserById((Long) sess.getAttribute("userId"));
			user.setPassword(null);
			request.setAttribute("user", user);
			if (user.getBirthDate() != null){
				String date = Localizator.printDate(new Date(user.getBirthDate()));
				
				request.setAttribute("birthDate", date);
			}
			page = "/home.jspx";
			

		} finally {
			userDao.close();
		}

		return page;
	}
}
